package com.example.walkingtour;

import java.io.File;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

public class PlayVideoActivity extends Activity implements
		OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener,
		OnVideoSizeChangedListener, SurfaceHolder.Callback {

	private static final String TAG = "playvideo";
	private SurfaceView mPreview;
	private SurfaceHolder holder;
	private Bundle extras;
	private String path;
	private MediaPlayer mMediaPlayer;
	private int mVideoWidth;
	private int mVideoHeight;
	private boolean mIsVideoSizeKnown = false;
	private boolean mIsVideoReadyToBePlayed = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_play_video);

		mPreview = (SurfaceView) findViewById(R.id.surface_view);

		holder = mPreview.getHolder();

		holder.addCallback(this);

		// holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

		extras = getIntent().getExtras();
		path = extras.getString("path");

	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		Log.d(TAG, "surfaceChanged called");
	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		Log.d(TAG, "surfaceCreated called");

		playVideo();
	}

	private void playVideo() {
		doCleanUp();
		try {
			if (path == "") {
				Toast.makeText(this, "Error reading file", Toast.LENGTH_LONG)
						.show();
				finish();
			}

			// Create a new media player and set the listeners

			mMediaPlayer = new MediaPlayer();
			File f = new File(path);
			boolean read = f.canRead();
			if (!read) {
				Toast.makeText(this, "Error reading file", Toast.LENGTH_LONG)
						.show();
				finish();
			}
			mMediaPlayer.setDataSource(path);
			mMediaPlayer.setDisplay(holder);
			mMediaPlayer.prepare();
			mMediaPlayer.setOnBufferingUpdateListener(this);
			mMediaPlayer.setOnCompletionListener(this);
			mMediaPlayer.setOnPreparedListener(this);
			mMediaPlayer.setOnVideoSizeChangedListener(this);
			mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

		}

		catch (Exception e) {

			Log.e(TAG, "error: " + e.getMessage(), e);
			e.printStackTrace();
		}
	}

	private void doCleanUp() {
		mVideoWidth = 0;
		mVideoHeight = 0;
		mIsVideoReadyToBePlayed = false;
		mIsVideoSizeKnown = false;
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		Log.d(TAG, "surfaceDestroyed called");
	}

	@Override
	public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
		Log.v(TAG, "onVideoSizeChanged called");
		if (width == 0 || height == 0) {
			Log.e(TAG, "invalid video width(" + width + ") or height(" + height
					+ ")");
			return;
		}
		mIsVideoSizeKnown = true;
		mVideoWidth = width;
		mVideoHeight = height;
		if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
			startVideoPlayback();
		}
	}

	private void startVideoPlayback() {
		Log.v(TAG, "startVideoPlayback");
		holder.setFixedSize(mVideoWidth, mVideoHeight);
		mMediaPlayer.start();
	}

	@Override
	public void onPrepared(MediaPlayer arg0) {
		Log.d(TAG, "onPrepared called");
		mIsVideoReadyToBePlayed = true;
		if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
			startVideoPlayback();
		}
	}

	public void onBufferingUpdate(MediaPlayer arg0, int percent) {
		Log.d(TAG, "onBufferingUpdate percent:" + percent);
	}

	public void onCompletion(MediaPlayer arg0) {
		Log.d(TAG, "onCompletion called");

		// this.mMediaPlayer.stop();
		finish();
	}

	/**
	 * Helper method to assist any activity that wants to utilize
	 * PlayVideoActivity to play a video from a file
	 * 
	 * @param context
	 *            Context it is being called from
	 * @param file
	 *            String containing absolute file path
	 * @return returns false if null parameters, or file can't be read returns
	 *         true otherwise
	 */
	public static boolean playVideo(Context context, File file) {
		if (context == null || file == null)
			return false;
		if (!file.exists())
			return false;
		if (!file.canRead())
			return false;
		Intent intent = new Intent(context, PlayVideoActivity.class);
		intent.putExtra("path", file.getAbsolutePath());
		context.startActivity(intent);
		return true;
	}

	/**
	 * Helper method to assist any activity that wants to utilize
	 * PlayVideoActivity to play a video from a file
	 * 
	 * @param context
	 *            Context it is being called from
	 * @param filePath
	 *            String containing absolute file path
	 * @return returns false if null parameters, or file can't be read returns
	 *         true otherwise
	 */
	public static boolean playVideo(Context context, String filePath) {
		return playVideo(context, new File(filePath));
	}
}
